$(function () {
    $("#jqGrid").Grid({
        url: '../contractTpl/list',
        colModel: [{
            label: 'id', name: 'id', index: 'id', key: true, hidden: true
        }, {
            label: '合同名称', name: 'tplName', index: 'tpl_name', width: 200
        }, {
            label: '模板首页图', name: 'tplImg', index: 'tpl_img', width: 80, formatter: function (value) {
                return transImg(value);
            }
        }, {
            label: '排序', name: 'sortOrder', index: 'sort_order', width: 80
        }, {
            label: '模板文件', name: 'tplUrl', index: 'tpl_url', width: 300, formatter: function (url) {
                if (url) {
                    return "<a href=\""+url+"\" target=\"_blank\">"+url+"</a>";
                } else {
                    return '-';
                }
            }
        }]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        contracttpl: {tplName: '',tplImg:'', tplUrl: '', sortOrder:1},
        ruleValidate: {
            tplName: [
                {required: true, message: '合同名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            tplName: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.contracttpl = {tplName: '',tplImg:'', tplUrl: '', sortOrder:1};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.contracttpl.id == null ? "../contractTpl/save" : "../contractTpl/update";
            console.log(JSON.stringify(vm.contracttpl));
            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.contracttpl),
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {

                Ajax.request({
                    type: "POST",
                    url: "../contractTpl/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../contractTpl/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.contracttpl = r.contracttpl;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSuccessAppListPicUrl: function (res, file) {
            vm.contracttpl.tplUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 PDF 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 10M。'
            });
        },
        handleImgFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleImgMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccessTplImg: function (res, file) {
            vm.contracttpl.tplImg = file.response.url;
        },
        eyeImageTplImg: function () {
            var url = vm.contracttpl.tplImg;
            eyeImage(url);
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});